/*
 * ------------------------------------------------------------------
 * 封装token操作 模块
 * ------------------------------------------------------------------
 * @关于我：
 * QQ:2815473064
 * wechat:13531609457
 * ------------------------------------------------------------------
 */
import config from "@/config";
const {
  // 用户鉴权方式, token / cookie
  userAuthenticationType,
  // 用户Token开启本地存储
  UserTokenStorage,
  // token存储位置 
  UserTokenSavePosition,
  // token存储的Key
  UserTokenSaveKey,
  UserTokenValueConceal
} = config

// localstorage 操作模块
import localStorageModule from "./localStorage";
// SessionStorage 操作模块
import SessionStorageModule from "./sessionStorage"

/**
 * 设置Token
 * **/
export function SetUserToken(SetUserTokenInfo = {}) {
  const { token, tokenExpire } = SetUserTokenInfo
  // 是否开启Token 本地存储
  if (!UserTokenStorage) { return }

  if (userAuthenticationType == "token") {
    if (UserTokenSavePosition == "localStorage") {
      localStorageModule.save({
        key: UserTokenSaveKey,
        value: token,
        encrypt: UserTokenValueConceal,
        expire: tokenExpire,
      })
    } else if (UserTokenSavePosition == "Sessionstorage") {
      SessionStorageModule.save({
        key: UserTokenSaveKey,
        value: token,
        encrypt: UserTokenValueConceal,
        expire: tokenExpire,
      })
    }
  }
}
/**
 *  删除保存的token
 * **/
export function removeToken() {
  // 是否开启Token 本地存储
  if (!UserTokenStorage) { return false }
  if (userAuthenticationType == "token") {
    if (UserTokenSavePosition == "localStorage") {
      localStorageModule.remove(UserTokenSaveKey)
    } else if (UserTokenSavePosition == "Sessionstorage") {
      SessionStorageModule.remove(UserTokenSaveKey)
    }
  }
}
/**
 *  获取Token信息
 * **/
export function getUserToken() {
  // return StorageGet(UserTokenSaveKey);
  let UserToken = null

  // 是否开启Token 本地存储
  if (!UserTokenStorage) { return UserToken }

  if (userAuthenticationType == "token") {
    if (UserTokenSavePosition == "localStorage") {
      UserToken = localStorageModule.get(UserTokenSaveKey)
      return UserToken
    } else if (UserTokenSavePosition == "Sessionstorage") {
      UserToken = SessionStorageModule.get(UserTokenSaveKey)
      return UserToken
    }
  }
}
export default {
  SetUserToken,
  removeToken,
  getUserToken,
};
